<?php
ini_set('display_errors', '1');

define('OAUTH2_PROXY_SERVER', "http://www.xunlianri.com");

//define('OAUTH2_PUSECRET', "7232f951220f5c632894c016c13a844a");
define('OAUTH2_APPID', "wx8ed1756092833c6a");
define('OAUTH2_APPSECRET', "ddca47dd196ace5421cb0b4499949514");

// function wxoauth2_request_userinfo_uri($base_uri){
// 	$backurl = $base_uri.'?backfrom=weiwangapp';
//     $redirect_uri = urlencode("http://testminisite.weiwangapp.com/wxoauth2/auth.php?base_uri=".$backurl);
// 	$oauth2_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".OAUTH2_APPID."&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
// 	return $oauth2_uri;
// }

function wxoauth2_request_openid($code){
		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".OAUTH2_APPID."&secret=".OAUTH2_APPSECRET."&code=$code&grant_type=authorization_code";
		$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// curl_setopt($ch, CURLOPT_TIMEOUT, 3);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

		$res = curl_exec($ch);
		// var_dump(curl_error($ch));
		// echo($res);

 		curl_close($ch);
 		return $res;
 
}

function wxoauth2_request_userinfo($access_token,$openid){
	    $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
//		$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".OAUTH2_APPID."&secret=".OAUTH2_APPSECRET."&code=$code&grant_type=authorization_code";
		$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
   		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	return curl_exec($ch);
}


function wxoauth2_request_openid_baseuri($base_uri){
    //$backurl = urlencode($base_uri);
    $backurl = $base_uri.'?backfrom=weiwangapp';
    $redirect_uri = urlencode("http://www.weiwangapp.com/wxoauth2/auth.php?base_uri=".$backurl);
	$oauth2_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7a5c555e1f6b56bf&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
	return $oauth2_uri;
}

function wxoauth2_request_openid_uri($base_uri){
	$redirect_uri = urlencode(OAUTH2_PROXY_SERVER."/index.php?g=Home&m=Oauth2&a=proxy&base_uri=".urlencode(C("site_url")."/wxoauth2/index.php?base_uri=$base_uri"));
	$oauth2_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7a5c555e1f6b56bf&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
	return $oauth2_uri;
}


function wxoauth2_request_openid_proxy($code){
	$rquest_time = time();
	$sign = md5(OAUTH2_PUSECRET. $rquest_time);
	$url = OAUTH2_PROXY_SERVER."/index.php?g=Home&m=Oauth2&a=openid&request_time=".$rquest_time."&sign=".$sign."&code=".$code;
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
}

function wxoauth2_request_userinfo_proxy($code){
	$rquest_time = time();
	$sign = md5(OAUTH2_PUSECRET. $rquest_time);
	$url = OAUTH2_PROXY_SERVER."/index.php?g=Home&m=Oauth2&a=userinfo&request_time=".$rquest_time."&sign=".$sign."&code=".$code;
	$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($ch);
}